<template>
  <BasicModal
    v-bind="$attrs"
    :title="title ? `${title} - ${formModeEntityRef.Display}` : formModeEntityRef.Display"
    width="800px"
    @register="registerModal"
  >
    <Description @register="registerDescription" />
  </BasicModal>
</template>

<script lang="ts">
  import { defineComponent, ref } from 'vue';
  import { BasicModal, useModalInner } from '@/components/Modal';
  import { Description, useDescription } from '@/components/Description/index';
  import { FormModeEnum, FormModeEntity } from '@/enums/formEnum';
  import { propTypes } from '@/utils/propTypes';
  import { AdColumn, useAdRender } from '@/components/AdTable';
  import type { DescItem } from '@/components/Description/index';
  import { GetMaterialTreeNodes, GetDictNodes } from './ipn.api';
  import { getStatusSchema } from './ipn.enum';

  export default defineComponent({
    name: 'Ipndesc',
    components: {
      BasicModal,
      Description,
    },
    props: {
      /** 表单抬头 */
      title: propTypes.string,
      unitDicts: propTypes.array,
      projectDicts: propTypes.array,
    },
    setup(props) {
      //let unitDicts = ref<any[]>([]);
      const formModeEntityRef = ref<FormModeEntity>(new FormModeEntity(FormModeEnum.Ok));
      const [registerModal, modalMethods] = useModalInner((data: Recordable) => {
        try {
          modalMethods.setModalProps({
            canFullscreen: true,
            centered: false,
            maskClosable: false,
            destroyOnClose: true,
            width: '50%',
          });
          modalMethods.changeLoading(true);
          // unitDicts.value = (await GetDictNodes('unit')).Data;

          if (data.record) {
            descMethods.setDescProps({
              data: {
                ...data.record,
              },
            });
          }
        } finally {
          modalMethods.changeLoading(false);
        }
      });

      //#region 数据定义、事件处理
      const statusEnumSchema = getStatusSchema();
      const descSchema: DescItem[] = [
        {
          field: 'IpnNo',
          label: 'IPN号',
        },
        {
          field: 'Status',
          label: '状态',
          render: (value, record) => {
            return useAdRender.renderEnum(value, statusEnumSchema);
          },
        },
        {
          field: 'MaterialCategoryName',
          label: '类别名称',
        },
        {
          field: 'Name',
          label: '部件名称',
        },
        {
          field: 'Unit',
          label: '单位',
          render: (value, record) => {
            return useAdRender.renderSelect(value, props.unitDicts, 'CodeValue', 'CodeName');
          },
        },
        {
          field: 'Name',
          label: '部件名称',
        },
        {
          field: 'Description',
          label: '规格描述',
        },
        {
          field: 'Manufacturer',
          label: '生产商',
        },
        {
          field: 'StockAttributes',
          label: '存货属性',
        },
        {
          field: 'Project',
          label: '所属项目',
          render: (value, record) => {
            return useAdRender.renderSelect(value, props.projectDicts, 'CodeValue', 'CodeName');
          },
        },
        {
          field: 'Remark',
          label: '备注',
        },
        {
          field: 'CreatedTime',
          label: '创建时间',
          render: (value, record) => {
            return useAdRender.renderDate(value);
          },
        },
      ];

      //#endregion

      const [registerDescription, descMethods] = useDescription({
        column: 1,
        schema: descSchema,
      });

      return {
        registerModal,
        registerDescription,
        formModeEntityRef,
      };
    },
  });
</script>
